#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

class Solution{
    public:
    int ans1(vector<int>& ratings){
        
        int length = ratings.size();
        vector<int> candys1(length, 1);
        vector<int> candys2(length, 1);

        for(int i = 0; i != length-1; i++){
            int j = length-i-1;
            if(ratings[i] < ratings[i+1]){
                candys1[i+1] = candys1[i] + 1;
            }else{
                candys1[i+1] = 1;
            }
            if(ratings[j-1] > ratings[j]){
                candys2[j-1] = candys2[j] + 1;
            }else{
                candys2[j-1] = 1;
            }
        }
        int candy = 0;
        for(int i = 0; i != length; i++){
            candy = candy + max(candys1[i], candys2[i]);
        }

        return candy;

    }
};

void test1(){
    Solution s;
    vector<int> rat = {1, 0, 2};
    int ret = s.ans1(rat);
    cout << ret << endl;
}

void test2(){
    Solution s;
    vector<int> rat = {1, 2, 2};
    int ret = s.ans1(rat);
    cout << ret << endl;
}

void test3(){
    Solution s;
    vector<int> rat = {4, 3, 1, 2, 2, 4, 5, 1, 6, 6, 2, 2};
    int ret = s.ans1(rat);
    cout << ret << endl;
}

int main(void){
    test1();
    test2();
    test3();

    return 0;
}